Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Latest commit

 

History

History
24 lines (18 loc) · 1.07 KB

7.1.1 - Http/Server->on.md

File metadata and controls

24 lines (18 loc) · 1.07 KB

Http\Server->on

注册事件回调函数,与Server->on相同,不同之处是:

  • Http\Server->on不接受onConnect/onReceive回调设置
  • Http\Server->on额外接受1种新的事件类型onRequest

onRequest事件

$http_server->on('request', function(swoole_http_request $request, swoole_http_response $response) {
     $response->end("<h1>hello swoole</h1>");
})

在收到一个完整的Http请求后,会回调此函数。回调函数共有2个参数:

  • $requestHttp请求信息对象,包含了header/get/post/cookie等相关信息
  • $responseHttp响应对象,支持cookie/header/statusHttp操作
  • onRequest回调函数返回时底层会销毁$request$response对象,如果未执行$response->end()操作,底层会自动执行一次$response->end("")

onRequest1.7.7或更高版本可用
$response/$request对象传递给其他函数时,不要加&引用符号
$response/$request对象传递给其他函数后,引用计数会增加,onRequest退出时不会销毁